package mhcs.client;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.http.client.URL;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.jsonp.client.JsonpRequestBuilder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
/**
 * Displays current weather information including
 * temperature, visibility and time of sunrise/sunset.
 * @author trimarkert
 *
 */
public class WeatherWidget extends Composite {
	
	/**
	 * This is the constructor which asks for JSON data and returns
	 * information for the weather as labels in a panel.
	 */
	public WeatherWidget() {
		

		String url = "http://api.wunderground.com/" 
				+ "api/bc7b74da79286894/conditions/q/55812.json?";
				url = URL.encode(url); 
				
		String astroURL = "http://api.wunderground.com/api/bc7b74da79286894/astronomy/q/Australia/Sydney.json";
				
				
				final JsonpRequestBuilder jsonp = new JsonpRequestBuilder(); 
				jsonp.setCallbackParam("callback");
				final HorizontalPanel vp = new HorizontalPanel();
				initWidget(vp); 
				jsonp.requestObject(url, 
						new AsyncCallback<JavaScriptObject>() { 
					
					public void onFailure(final Throwable caught) { 
						Window.alert("JSONP onFailure"); 
					} 
					public void onSuccess(final JavaScriptObject s) { 
						JSONObject obj = new JSONObject(s); 
						String result = obj.toString();
						 String sAll = result; 
						 JSONObject jA = 
						 (JSONObject) JSONParser.parseLenient(sAll); 
						 JSONValue jTry = jA.get("current_observation"); 
						 
						 JSONObject jB = 
						 (JSONObject) JSONParser.parseLenient(jTry.toString()); 
						 JSONValue temp = jB.get("temp_c"); 
						 JSONValue visibility = jB.get("visibility_km"); 
						 
						 String sTemp = "Current temperature: " 
								 + temp.toString(); 
						 String sVisibility = "Current Visibility: " 
								 + visibility.toString();
						 Image image = new Image("images/wunder.jpg");
						 final int pixH = 65;
						 final int pixW = 50;
				         image.setPixelSize(pixH, pixW);
				         vp.add(image);
						 vp.add(new Label(sTemp)); //TO VIEW 
						 vp.add(new Label(sVisibility)); //TO VIEW 
					 	} 
				});
				jsonp.requestObject(astroURL, new AsyncCallback<JavaScriptObject>(){
					public void onFailure(final Throwable caught){
						Window.alert("JSONP onFailure");
					}
					public void onSuccess(final JavaScriptObject s){
						JSONObject obj = new JSONObject(s);
						String result = obj.toString();
						String sAll = result;
						JSONObject jA =
								(JSONObject) JSONParser.parseLenient(sAll);
						JSONValue jTry = jA.get("moon_phase");
						
						JSONObject jB =
								(JSONObject) JSONParser.parseLenient(jTry.toString());
						JSONValue sunsetTime = jB.get("sunset");
						String temp = sunsetTime.toString();
						
						JSONObject jC =
								(JSONObject) JSONParser.parseLenient(temp);
						JSONValue actualTime = jC.get("hour");
						String tempTwo = actualTime.toString();
						
						String sunTemp = "Sunset time: " + tempTwo;
						vp.add(new Label(sunTemp));
					}
				});
				
	}
}
